package ltu.m7011e.server.webservice.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import ltu.m7011e.server.response.GlobalResponse;
import ltu.m7011e.server.response.LinkPost;
import ltu.m7011e.server.response.link.LinkResponse;
import ltu.m7011e.server.service.impl.LinkService;
import ltu.m7011e.server.service.impl.LoginService;
import ltu.m7011e.server.webservice.ILinkWebService;

@Path("/link")
public class LinkWebService implements ILinkWebService {
	
	private LinkService myLinkService = new LinkService();
	
	/**
	 * {@inheritDoc}
	 */
	@GET
	@Path("/getNewestLinks")
	@Produces("application/XML")
	public GlobalResponse GetNewestLinks(@QueryParam("numberOfRecords") int numberOfRecords,@QueryParam("firstResult") int firstResult){
		
		GlobalResponse response = new GlobalResponse();
		response.getLinksAction = myLinkService.loadNewestLinks(numberOfRecords,firstResult);
		
		return response;
	}
	/**
	 * {@inheritDoc}
	 */
	@GET
	@Path("/getLink")
	@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
	public GlobalResponse GetLink(@QueryParam("numberOfRecords") int numberOfRecords,@QueryParam("firstResult") int firstResult,@QueryParam("category") String cat){
		
		GlobalResponse response = new GlobalResponse();
		response.getLinksAction = myLinkService.loadNewestLinksByCat(numberOfRecords,firstResult,cat);
		// TODO call service loadNewestLinks() ? directly or can Inject ?
		
		return response;
	}
	/**
	 * {@inheritDoc}
	 */

	@GET
	@Path("/loadLogin")
	@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
	public GlobalResponse loadLogin(){
		
		LoginService hej = new LoginService();
		GlobalResponse response = new GlobalResponse();
		response.userInfoAction = hej.LoadLogin();
		
		// TODO call service loadNewestLinks() ? directly or can Inject ?
		
		
		return response;
	}
	@GET
	@Path("/getTopLinks")
	@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
	public GlobalResponse GetTopLinks(@QueryParam("numberOfRecords") int numberOfRecords,@QueryParam("firstResult") int firstResult){
		
		GlobalResponse response = new GlobalResponse();
		response.getLinksAction = myLinkService.loadTopLinks(numberOfRecords,firstResult);
		// TODO call service loadNewestLinks() ? directly or can Inject ?
		
		
		return response;
	}
	
	
	
	@POST
	@Path("/postLink")
	@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
	public Response PostLink(LinkPost newLink){ 
		
		
		if ( newLink != null ){
			LinkResponse linkToPost = newLink.linkToPost;
			try {
				if ( myLinkService.doPost(linkToPost) ){
	         		return Response.ok().build();
				}else{
					return Response.serverError().build();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return Response.serverError().build();
			}
		}else{
			return Response.status(javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE).build();
		}

	}
	
	@GET
	@Path("/likeLink")
	public Response SendLikeToServer(@QueryParam("linkId") Integer linkId){
		
		
		int state = myLinkService.addLikeTo(linkId);
				
		if (state == 2)
		{
		 return Response.status(200).build();	
		
		}else if (state == 1) { return Response.status(406).build();}
		else return Response.status(412).build();
	}
	
}
